package org.ManagerSystem.web;

import org.ManagerSystem.DTO.LoginResult;
import org.ManagerSystem.DTO.PageBean;
import org.ManagerSystem.entity.Department;
import org.ManagerSystem.entity.Employee;
import org.ManagerSystem.entity.Role;
import org.ManagerSystem.services.DepartmentService;
import org.ManagerSystem.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.ManagerSystem.services.EmployeeService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/")
public class ManagerController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RoleService roleService;

    //用于验证登录的方法
    @RequestMapping(value = "login" , method = RequestMethod.POST)
    //开启对于 ajax 请求的支持,返回 json
    @ResponseBody
    public LoginResult login(HttpServletRequest request , HttpServletResponse response , Employee employee){
        //类似于 struts2 的模型驱动的东西，页面上的输入框的 name 和实体类中的属性名称对应时，自动赋值
        LoginResult result = employeeService.login(employee);
        if(result.isLogin() == true){
            HttpSession session = request.getSession();
            session.setAttribute("eusername" , employee.getEusername());
            Cookie eusername = new Cookie("eusername" , employee.getEusername());
            response.addCookie(eusername);
        }
        return result;
    }

    //返回详情页
    @RequestMapping(value = "{eusername}/detail")
    public String loginSuccess(@PathVariable("eusername") String eusername , Model model){
        Employee employee = employeeService.queryByEusername(eusername);
        model.addAttribute("employee" , employee);
        return "detail";
    }

    //处理注销请求
    @RequestMapping(value = "{eusername}/detail/logout")
    public String logOut(HttpServletRequest request , @PathVariable("eusername") String eusername){
        //设置 session 立即过期
        request.getSession().invalidate();
        //删除 cookie 中的相应数据
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie : cookies){
            if("eusername".equals(cookie.getName())){
                cookie.setMaxAge(0);
            }
        }
        return "logout";
    }

    //返回个人信息页
    @RequestMapping(value = "{eusername}/detail/personal")
    public String personal(long eid , Model model){
        Employee employee = employeeService.queryById(eid);
        model.addAttribute("employee" , employee);
        return "personal";
    }

    //返回部门信息页
    @RequestMapping(value = "{eusername}/detail/department" , method = RequestMethod.POST)
    public String department(Department departmentIn , Model model){
        Department department = departmentService.queryById(departmentIn.getDid());
        model.addAttribute("department" , department);
        return "department";
    }

    //返回所有部门页
    @RequestMapping(value = "{eusername}/detail/allDepartment" , method = RequestMethod.POST)
    public String allDepartment(int currentPage , Model model){
        PageBean<Department> pageBean = departmentService.queryByPage(currentPage);
        model.addAttribute("pageBean" , pageBean);
        return "allDepartment";
    }

    //返回员工列表页
    @RequestMapping(value = "{eusername}/detail/employeeList" , method = RequestMethod.POST)
    public String employeeList(int rid , int currentPage , long did ,  Model model){
        PageBean<Employee> pageBean = employeeService.employeeList(rid , currentPage , did);
        model.addAttribute("pageBean" , pageBean);
        return "employeeList";
    }

    //查询添加的员工的用户名和编号是否存在
    @RequestMapping(value = "{eusername}/detail/query/Exist" , method = RequestMethod.POST)
    @ResponseBody
    public String queryExist(Employee employee , Model model){
        return employeeService.queryExist(employee);
    }

    //返回添加部门页面
    @RequestMapping(value = "{eusername}/detail/add/Department" , method = RequestMethod.POST)
    public String addDepartment(){
        return "addDepartment";
    }

    //返回添加员工页面
    @RequestMapping(value = "{eusername}/detail/add/Employee" , method = RequestMethod.POST)
    public String addEmployee(long rid, long did, Model model){
        System.out.println(rid);
        List<Department> departmentList = departmentService.queryAll(rid,did);
        List<Role> roleList = roleService.queryAll(rid);
        model.addAttribute("departmentList" , departmentList);
        model.addAttribute("roleList" , roleList);
        model.addAttribute("rid",rid);
        model.addAttribute("did",did);
        return "addEmployee";
    }

    //保存部门并返回部门列表
    @RequestMapping(value = "{eusername}/detail/save/Department" , method = RequestMethod.POST)
    public String saveDepartment(@PathVariable("eusername") String eusername , Department department){
        departmentService.save(department);
        return "forward:/"+eusername+"/detail/allDepartment?currentPage=1";
    }

    //保存员工并返回员工列表
    @RequestMapping(value = "{eusername}/detail/save/Employee" , method = RequestMethod.POST)
    public String saveEmployee(@PathVariable("eusername") String eusername , Employee employee , long rid , long did){
        employeeService.save(employee);
        return "forward:/"+eusername+"/detail/employeeList?rid="+rid+"&did="+did+"&currentPage=1";
    }

    //删除部门方法
    @RequestMapping(value = "{eusername}/detail/delete/Department" , method = RequestMethod.POST)
    public String deleteDepartment(@PathVariable("eusername") String eusername , long did){
        departmentService.deleteById(did);
        //TODO 将删除后返回的页面定位到之前所在的页面
        return "forward:/"+eusername+"/detail/allDepartment?currentPage=1";
    }

    //删除员工方法
    @RequestMapping(value = "{eusername}/detail/delete/Employee" , method = RequestMethod.POST)
    public String deleteEmployee(@PathVariable("eusername") String eusername , long eid , long rid , long did){
        employeeService.deleteById(eid);
        return "forward:/"+eusername+"/detail/employeeList?rid="+rid+"&did="+did+"&currentPage=1";
    }

    //修改部门页面
    @RequestMapping(value = "{eusername}/detail/edit/Department" , method = RequestMethod.POST)
    public String editDepartment(long did , int currentPage , Model model){
        Department department = departmentService.queryById(did);
        model.addAttribute("department" , department);
        model.addAttribute("currentPage" , currentPage);
        return "editDepartment";
    }

    //保存修改部门
    @RequestMapping(value = "{eusername}/detail/update/Department" , method = RequestMethod.POST)
    public String updateDepartment(@PathVariable("eusername") String eusername , Department department){
        departmentService.update(department);
        //TODO 将保存修改后的页面定位到修改前打开的页面
        return "forward:/"+eusername+"/detail/allDepartment?currentPage=1";
    }

    //修改员工页面
    @RequestMapping(value = "{eusername}/detail/edit/Employee" , method= RequestMethod.POST)
    public String editEmployee(long eid , long rid , long did , Model model){
        Employee employee = employeeService.queryById(eid);
        List<Department> departmentList = departmentService.queryAll(did , rid);
        List<Role> roleList = roleService.queryAll(rid);
        model.addAttribute("employee" , employee);
        model.addAttribute("departmentList" , departmentList);
        model.addAttribute("roleList" , roleList);
        model.addAttribute("rid" , rid);
        model.addAttribute("did" , did);
        return "editEmployee";
    }

    //保存修改员工
    @RequestMapping(value = "{eusername}/detail/update/Employee" , method = RequestMethod.POST)
    public String updateEmployee(@PathVariable("eusername") String eusername , Employee employee , long rid , long did){
        employeeService.update(employee);
        return "forward:/"+eusername+"/detail/employeeList?rid="+rid+"&did="+did+"&currentPage=1";
    }

    //修改密码页面
    @RequestMapping(value = "{eusername}/detail/edit/Password" , method = RequestMethod.POST)
    public String editPassword(Employee employee , Model model){
        model.addAttribute("employee", employee);
        return "editPassword";
    }

    //保存修改的密码
    @RequestMapping(value = "{eusername}/detail/update/Password" , method = RequestMethod.POST)
    public String updatePassword(@PathVariable("eusername") String eusername , Employee employee , Model model){
        employeeService.updatePassword(employee);
        return "forward:/"+eusername+"/detail/personal?eid="+employee.getEid();
    }
}
